<?php

	include_once('classes/view.php');
	//include_once('JavaScripts/scripts.js');
	
	View::displayHeader();
	
	if (isset($_SESSION['user_id']) && $_SESSION['user_id'] != ''){
		echo '<br /><br /> Jesteś już zarejestrowany i zalogowany <br /><br />';
	} else {
		ShowRegisterForm();
	}

	View::displayFooter();
	die();

	function ShowRegisterForm(){
		if(!isset($_SESSION['user_login'])) $_SESSION['user_login'] = "";
		if(!isset($_SESSION['user_haslo1'])) $_SESSION['user_haslo1'] = "";
		if(!isset($_SESSION['user_haslo2'])) $_SESSION['user_haslo2'] = "";
		if(!isset($_SESSION['user_imie'])) $_SESSION['user_imie'] = "";
		if(!isset($_SESSION['user_nazwisko'])) $_SESSION['user_nazwisko'] = "";
		if(!isset($_SESSION['user_email'])) $_SESSION['user_email'] = "";
		
		echo '
		<script type="text/javascript">
			function validateOnSubmit() {
		    	var elem;
		    	var myCountErrors=0;
		    		    	
		    	if (!validatePresent (document.forms.register.login,  \'inf_login\'))        myCountErrors += 1;
		    	if (!validatePresent (document.forms.register.imie,  \'inf_imie\'))        myCountErrors += 1;
		    	if (!validatePresent (document.forms.register.nazwisko,  \'inf_nazwisko\'))        myCountErrors += 1;
		    	if (!validatePassword(document.forms.register.haslo1, document.forms.register.haslo2, \'inf_haslo1\', \'inf_haslo2\')) myCountErrors += 2;
		    	if (!validateEmail   (document.forms.register.email, \'inf_email\', true))  myCountErrors += 1; 
		    	 
		    	if (myCountErrors > 1)  alert(\'Należy poprawić kilka pól.\');
		    	else if (myCountErrors == 1) alert(\'Należy poprawić jedno pole.\');
		
		    	return (myCountErrors == 0);
		  	};
		</script>
		
		<form name="register" method="post" onsubmit="return validateOnSubmit()" action="add_user.php">
			<div class="box">
			  <h1>Formularz rejestracyjny</h1>
			  <label>
			    <span>Podaj login: </span>
			    <input class="wpis" type="text" name="login" id="login" size="70" maxlength="30" onchange="validatePresent(this, \'inf_login\');" value="' . $_SESSION['user_login'] . '" />
			    <i id="inf_login">Pole wymagane</i>
			  </label>
			
			  <label>
			    <span>Podaj hasło: </span>
			    <input class="wpis" type="password" name="haslo1" id="haslo1" size="70" maxlength="50" value="' . $_SESSION['user_haslo1'] . '"/>
			    <i id="inf_haslo1">Pole wymagane</i>
			  </label>
			  
			  <label>
			    <span>Ponownie: </span>
			    <input class="wpis" type="password" name="haslo2" id="haslo2" size="70" maxlength="50" value="' . $_SESSION['user_haslo2'] . '"/>
			    <i id="inf_haslo2">Pole wymagane</i>
			  </label>
			  
			  <label>
			    <span>Podaj imię: </span>
			    <input class="wpis" type="text" name="imie" id="imie" size="70" maxlength="30" onchange="validatePresent(this, \'inf_imie\');" value="' . $_SESSION['user_imie'] . '"/>
			    <i id="inf_imie">Pole wymagane</i>
			  </label>
			  
			  <label>
			    <span>Podaj nazwisko: </span>
			    <input class="wpis" type="text" name="nazwisko" id="nazwisko" size="70" maxlength="40" onchange="validatePresent(this, \'inf_nazwisko\');" value="' . $_SESSION['user_nazwisko'] . '"/>
			    <i id="inf_nazwisko">Pole wymagane</i>
			  </label>
			  
			  <label>
			    <span>Adres E-mail:</span>
			    <input class="wpis" type="text" name="email" id="email" size="70" maxlength="70" onchange="validateEmail(this, \'inf_email\', true);" value="' . $_SESSION['user_email'] . '" />
			    <i id="inf_email">Pole wymagane</i>
			  </label>
			
			  <label>
			    <input class="button" type="Submit" name="Submit" value="Wyślij" />
			  </label>
			
			</div>
		</form>
		<br /><br /><br />
		';
	}

?>